package com.itbaizhan.openhis.controller.erp;

import com.itbaizhan.openhis.dto.ProducerDto;
import com.itbaizhan.openhis.dto.RegisteredItemDto;
import com.itbaizhan.openhis.service.ProducerService;
import com.itbaizhan.openhis.utils.ShiroSecurityUtils;
import com.itbaizhan.openhis.vo.AjaxResult;
import com.itbaizhan.openhis.vo.DataGridView;
import org.apache.dubbo.config.annotation.Reference;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

@RequestMapping("erp/producer")
@RestController
public class ProducerController {

    @Reference
    private ProducerService producerService;

    /**
     * 分页查询生产厂家
     * @param producerDto
     * @return
     */
    @GetMapping("listProducerForPage")
    public AjaxResult listProducerForPage(ProducerDto producerDto){
        DataGridView dataGridView = producerService.listProducerForPage(producerDto);
        return AjaxResult.success("查询成功",dataGridView.getData(),dataGridView.getTotal());
    }

    /**
     * 添加生产厂家
     * @param producerDto
     * @return
     * 要先登录，再添加，否则postman报错，因为ShiroSecurityUtils.getCurrentSimpleUser()没有数据
     * 这里报空异常
     */
    @PostMapping("addProducer")
    public AjaxResult addProducer(@Validated ProducerDto producerDto){
        producerDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(producerService.addProducer(producerDto));
    }

    /**
     * 修改生产厂家
     * @param producerDto
     * @return
     */
    @PutMapping("updateProducer")
    public AjaxResult updateProducer(@Validated ProducerDto producerDto){
        producerDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(producerService.updateProducer(producerDto));
    }

    /**
     * 根据id查询生产厂家
     * @param producerId
     * @return
     */
    @GetMapping("getProducerById/{producerId}")
    public AjaxResult getProducerById(@PathVariable @Validated
                                      @NotNull(message = "id不能为空") Long producerId){
        return  AjaxResult.success(producerService.getOne(producerId));

    }

    /**
     * 根据id批量删除厂家
     * @param producerIds
     * @return
     */
    @DeleteMapping("deleteProducerByIds/{producerIds}")
    public AjaxResult deleteProducerByIds(@PathVariable @Validated
                                      @NotEmpty(message = "ids不能为空") Long[] producerIds){
        return  AjaxResult.toAjax(producerService.deleteProducerByIds(producerIds));

    }
}
